package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @projectName: heima-leadnews-project_training
 * @author: Tyoui
 * @description: 敏感词管理业务实现层
 * @date: 2023/8/2 9:11
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    /**
     * @param dto:
     * @return ResponseResult
     * @description 查询敏感词列表
     */
    @Override
    public ResponseResult list(SensitiveDto dto) {
        // 检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 组装查询条件
        LambdaQueryWrapper<WmSensitive> lqw = Wrappers.<WmSensitive>lambdaQuery();

        // 关键字模糊查询
        if (StringUtils.isNotBlank(dto.getName())) {
            lqw.like(WmSensitive::getSensitives, dto.getName());
        }
        // 按创建时间倒序查询
        lqw.orderByDesc(WmSensitive::getCreatedTime);
        // 分页查询
        IPage<WmSensitive> iPage = new Page<>(dto.getPage(), dto.getSize());
        page(iPage, lqw);
        // 封装结果返回
        List<WmSensitive> wmSensitiveList = iPage.getRecords(); // 当前页数据
        long total = iPage.getTotal(); // 总记录条数
        ResponseResult result = new PageResponseResult((int)iPage.getCurrent(), (int)iPage.getSize(), (int)total);
        result.setData(wmSensitiveList);
        return result;
    }

    /**
     * @param sensitive :
     * @return ResponseResult
     * @description 新增敏感词
     */
    @Override
    public ResponseResult saveSensitives(WmSensitive sensitive) {
        // 检查参数
        if (sensitive == null || StringUtils.isBlank(sensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive sensitive1 = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, sensitive.getSensitives()));
        // 判断是否重复
        if (sensitive1 != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 拷贝属性
        sensitive.setCreatedTime(new Date());
        // 新增数据
        save(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * @param id:
     * @return ResponseResult
     * @description 删除敏感词
     */
    @Override
    public ResponseResult delete(Integer id) {
        WmSensitive wmSensitive = getById(id);
        // 查询是否存在
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * @param sensitive :
     * @return ResponseResult
     * @description 修改敏感词
     */
    @Override
    public ResponseResult updateSensitives(WmSensitive sensitive) {
        // 检查参数
        if (sensitive == null || sensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 查询是否存在
        WmSensitive sensitive1 = getById(sensitive.getId());
        if (sensitive1 == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 拷贝属性
        updateById(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}